package org.openurp.edu.student.info.web.helper

import org.beangle.data.transfer.TransferResult
import org.beangle.data.transfer.importer.listener.ItemImporterListener
import org.openurp.edu.student.info.model.Schooling
import org.openurp.edu.student.info.model.Userstate
import org.beangle.commons.lang.Strings
import org.openurp.edu.student.info.model.User
import org.beangle.commons.lang.Numbers
import org.openurp.edu.student.info.model.Semester
import org.beangle.data.dao.OqlBuilder
import org.beangle.data.dao.EntityDao

class ImportSchoolingListener(entityDao: EntityDao) extends ItemImporterListener {
  override def onItemStart(tr: TransferResult) {
    val code = importer.curData.get("code").get
    val semesterCode = importer.curData.get("semester.code").get.toString()
    val semesterBuilder = OqlBuilder.from(classOf[Semester], "s").where("s.code=:code ", semesterCode)
    val semesters = entityDao.search(semesterBuilder)
    if (semesters.isEmpty) {
      tr.addFailure("学期数据格式非法", semesterCode)
    } else {
      val builder = OqlBuilder.from(classOf[Schooling], "s").where("s.code=:code and s.semester.code=:code", code, semesterCode)
      entityDao.search(builder) foreach { s => this.importer.current = s }
      this.importer.current.asInstanceOf[Schooling].semester = semesters.head
    }
  }

  override def onItemFinish(tr: TransferResult) {
    val std = tr.transfer.current.asInstanceOf[Schooling]
    entityDao.saveOrUpdate(std)

  }

}